package top.wilbert.collegelink.forum.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.wilbert.collegelink.common.constant.ForumConstant;
import top.wilbert.collegelink.common.enums.TargetTypeEnum;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.Query;

import top.wilbert.collegelink.common.utils.R;
import top.wilbert.collegelink.common.vo.UserSimpleInfoVo;
import top.wilbert.collegelink.common.vo.es.TalkEsModel;
import top.wilbert.collegelink.forum.dao.CommentDao;
import top.wilbert.collegelink.forum.dao.TalkDao;
import top.wilbert.collegelink.forum.entity.CommentEntity;
import top.wilbert.collegelink.forum.entity.TalkEntity;
import top.wilbert.collegelink.forum.feign.SearchFeignService;
import top.wilbert.collegelink.forum.feign.UserFeignService;
import top.wilbert.collegelink.forum.service.TalkService;
import top.wilbert.collegelink.forum.service.ThumbService;
import top.wilbert.collegelink.forum.vo.TalkResponseVo;


@Service("talkService")
public class TalkServiceImpl extends ServiceImpl<TalkDao, TalkEntity> implements TalkService {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private ThumbService thumbService;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private SearchFeignService searchFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TalkEntity> page = this.page(
                new Query<TalkEntity>().getPage(params),
                new QueryWrapper<TalkEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils selectSchoolTalkList(IPage<TalkResponseVo> page, Integer schoolId, Long userId) {

        List<TalkResponseVo> talkResponseVos = baseMapper.selectSchoolTalkList(page, schoolId);
        List<Long> userIds = new ArrayList<>();
        for (TalkResponseVo talkResponseVo : talkResponseVos) {
            userIds.add(talkResponseVo.getCreatorId());
            Boolean like = thumbService.isLike(talkResponseVo.getTalkId(), TargetTypeEnum.TALK.getTargetType(), userId);
            talkResponseVo.setIsLike(like);
        }

        Map<Long, UserSimpleInfoVo> map = userFeignService.searchUserByIds(userIds);

        for (TalkResponseVo talkResponseVo : talkResponseVos) {
            talkResponseVo.setUserSimpleInfoVo(map.get(talkResponseVo.getCreatorId()));
        }

        IPage<TalkResponseVo> iPage = page.setRecords(talkResponseVos);
        return new PageUtils(iPage);
    }

    @Override
    public void reduceCommentCount(Long commentId) {
        CommentEntity commentEntity = commentDao.selectById(commentId);
        baseMapper.reduceCommentCount(commentEntity.getTargetId());
    }

    @Override
    public void addLikeCount(Long talkId) {
        baseMapper.addLikeCount(talkId);
    }

    @Override
    public void reduceLikeCount(Long talkId) {
        baseMapper.reduceLikeCount(talkId);
    }

    @Override
    public void addViewCount(Long talkId) {
        baseMapper.addViewCount(talkId);
    }

    @Override
    public void open(Long talkId) {
        TalkEntity talkEntity = baseMapper.selectById(talkId);
        //封装talkEs信息
        TalkEsModel talkEsModel = new TalkEsModel();
        BeanUtils.copyProperties(talkEntity, talkEsModel);
        ArrayList<Long> list = new ArrayList<>();
        list.add(talkEntity.getCreatorId());
        Map<Long, UserSimpleInfoVo> userSimpleInfoVoMap = userFeignService.searchUserByIds(list);
        talkEsModel.setUserSimpleInfoVo(userSimpleInfoVoMap.get(talkEntity.getCreatorId()));

        //将数据发送给es进行保存
        R r = searchFeignService.talkStatusOpen(talkEsModel);
        if (r.getCode() == 0) {
            baseMapper.updateTalkStatus(talkId, ForumConstant.StatusEnum.TALK_OPEN.getCode());
        } else {
            //TODO 接口幂等性：重试机制
        }
    }

    @Override
    public void close(Long talkId) {
        searchFeignService.talkStatusClose(talkId);
    }

    @Override
    public void close(Long talkId, Integer status) {
        R r = searchFeignService.talkStatusClose(talkId);
        if (r.getCode() == 0) {
            baseMapper.updateTalkStatus(talkId, status);
        } else {
            //TODO 接口幂等性：重试机制
        }
    }

    @Override
    public PageUtils selectMyDraftTalkList(IPage<TalkResponseVo> page, Long userId) {
        List<TalkResponseVo> talkResponseVos = baseMapper.selectMyDraftTalkList(page, userId);

        Map<Long, UserSimpleInfoVo> map = userFeignService.searchUserByIds(Arrays.asList(userId));
        for (TalkResponseVo talkResponseVo : talkResponseVos) {
            talkResponseVo.setUserSimpleInfoVo(map.get(talkResponseVo.getCreatorId()));
        }

        IPage<TalkResponseVo> iPage = page.setRecords(talkResponseVos);
        return new PageUtils(iPage);
    }

    @Override
    public PageUtils selectMyTalkList(IPage<TalkResponseVo> page, Long userId) {
        List<TalkResponseVo> talkResponseVos = baseMapper.selectMyTalkList(page, userId);

        Map<Long, UserSimpleInfoVo> map = userFeignService.searchUserByIds(Arrays.asList(userId));
        for (TalkResponseVo talkResponseVo : talkResponseVos) {
            talkResponseVo.setUserSimpleInfoVo(map.get(talkResponseVo.getCreatorId()));
        }

        IPage<TalkResponseVo> iPage = page.setRecords(talkResponseVos);
        return new PageUtils(iPage);
    }

    @Override
    public Long selectUserIdByTalkId(Long talkId) {
        Long userId = baseMapper.selectUserIdByTalkId(talkId);
        return userId;
    }


}